package com.leonzhangxf.spel.reference;

import com.leonzhangxf.spel.reference.bean.Inventor;
import com.leonzhangxf.spel.reference.bean.Society;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

/**
 * Constructors
 *
 * You can invoke constructors by using the new operator.
 * You should use the fully qualified class name for all but the primitive types (int, float, and so on) and String.
 *
 * @author leonzhangxf
 * @date 20190427
 */
public class Constructors {

    public static void main(String[] args) {
        SpelParserConfiguration parserConfiguration =
            new SpelParserConfiguration(true, true);
        SpelExpressionParser parser = new SpelExpressionParser(parserConfiguration);

        Inventor einstein = parser.parseExpression(
            "new com.leonzhangxf.spel.reference.bean.Inventor('Albert Einstein', 'German')")
            .getValue(Inventor.class);
        System.out.println(einstein);

        //create new inventor instance within add method of List
        Society society = new Society();
        parser.parseExpression("Members.add(new com.leonzhangxf.spel.reference.bean.Inventor" +
            "('Albert Einstein', 'German'))")
            .getValue(society);
        System.out.println(society.getMembers());
    }
}
